package com.wzq.controller.pc;

import com.wzq.dao.pojo.Code;
import com.wzq.dao.pojo.Favorites;
import com.wzq.dao.pojo.Result;
import com.wzq.service.IFavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/PC/favorites")
public class PCFavoritesController {

    @Autowired
    private IFavoritesService iFavoritesService;
    //   添加
    @PostMapping
    public Result save(@RequestBody Favorites favorites) {
        boolean flag = iFavoritesService.save(favorites);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    //根据空间名称、用户id、项目id检查是否存在


    //   修改
    @PutMapping
    public Result update(@RequestBody Favorites favorites) {
        boolean flag = iFavoritesService.updateById(favorites);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    //    id删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean flag = iFavoritesService.removeById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    //    获得id对应用户信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Favorites favorites = iFavoritesService.getById(id);
        Integer code = favorites != null ? Code.GET_OK : Code.GET_ERR;
        String msg = favorites != null ? "" : "数据查询失败，请重试！";
        return new Result(code,favorites,msg);
    }

    //     获得所有用户信息
    @GetMapping
    public Result getAll() {
        List<Favorites> list = iFavoritesService.list();
        System.out.println(list);
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }

}
